//
//  HMToolbarView.swift
//  EmotionKeybaord
//
//  Created by ZhuDong on 16/5/30.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMEmotionToolbarView: UIStackView {
    var btnClickBack:((btn: UIButton)->())?
    var selectedBtn: UIButton?
    var btns:[UIButton] = [UIButton]()
    override init(frame: CGRect) {
        super.init(frame: frame)
        axis = .Horizontal
        distribution = .FillEqually
        setupUI()
    }

    
    init(){
        super.init(frame: CGRectZero)
        axis = .Horizontal
        distribution = .FillEqually
        setupUI()
        self.btnClick(btns[0])
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        addBtn("compose_emotion_table_left", titil: "默认",tag:0)
        addBtn("compose_emotion_table_mid", titil: "Emoje",tag:1)
        addBtn("compose_emotion_table_right", titil: "小浪花",tag:2)
    }
    private func addBtn(imageName: String,titil: String,tag: Int){
        let btn = UIButton()
        btn.tag = tag
        btn.setBackgroundImage(UIImage(named: imageName + "_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage( named: imageName + "_selected"), forState: .Selected)
        btn.setTitle(titil, forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        self.addArrangedSubview(btn)
        btn.addTarget(self, action: #selector(HMEmotionToolbarView.btnClick(_:)), forControlEvents: .TouchUpInside)
        self.btns.append(btn)
    }
    @objc func btnClick(btn: UIButton){
        selectedBtn?.selected = false
        btn.selected = true
        selectedBtn = btn
        if btnClickBack != nil {
            self.btnClickBack!(btn: btn)
        }
    }
    func setToobarSelectedBtn(section:Int){
        let btn = self.btns[section]
        selectedBtn?.selected = false
        btn.selected = true
        selectedBtn = btn
    }
}
